# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=wineditline
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.208
pkgrel=1
pkgdesc="An EditLine API implementation for the native Windows Console (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'aur: mingw-w64-wineditline'
)
msys2_repository_url='https://github.com/ptosco/wineditline/'
license=('spdx:BSD-3-Clause')
url="https://mingweditline.sourceforge.io/"
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/ptosco/wineditline/archive/${_realname}-${pkgver}.tar.gz"
        '001-fix-installing.patch'
        '003-dont-link-with-def.patch'
        '004-add-pkgconfig.patch')
sha256sums=('2df14abed2fadebf6e20bc0853b8b9b01f736ea3a5402420e0192029c6a23d80'
            '821d0a36115832a76497dc6d7ea4b6e45e4ce73621030a59865f851309f8db34'
            '19077726758de7780cd68f3f47d8353516fd864ede1903d55280a1fb3dbe408d'
            '7586c2bfeed3f4ff0998c2958b2613671fbca731768d2d2ac39f91ca28bfca2a')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${_realname}-${_realname}-${pkgver}
  
  apply_patch_with_msg \
    001-fix-installing.patch \
    003-dont-link-with-def.patch \
    004-add-pkgconfig.patch
}

build() {
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -S ${_realname}-${_realname}-${pkgver} \
    -B build-${MSYSTEM}

  cmake --build build-${MSYSTEM}
}

package() {
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}

  # Rename the static library
  mv "${pkgdir}"${MINGW_PREFIX}/lib/libedit_static.a "${pkgdir}"${MINGW_PREFIX}/lib/libedit.a

  sed -i ${_realname}-${_realname}-${pkgver}/${_realname}.pc -e "s,^prefix=\${prefix},prefix=${MINGW_PREFIX}," -e "s,Version: \${version},Version: ${pkgver},"
  install -Dm644 ${_realname}-${_realname}-${pkgver}/${_realname}.pc "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc

  install -Dm644 ${_realname}-${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
